<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head th:replace="~{sys/common/common_res::common_head('Dcoinpay|币种')}"></head>

<body>
<div class="panel panel-default operation" hidden>
  <div class="panel-heading title"></div>
<div class="layui-card-body">
<form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px">
    <input name="id" type = "hidden"/>
    <input name="nodeId" id = "nodeId" type = "hidden"/>
    <div class="layui-form-item">
      <label class="layui-form-label">币种代码</label>
      <div class="layui-input-block">
        <input type="code" name="code" placeholder="请输入币种代码" autocomplete="off" class="layui-input" lay-verify="required">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">币种全称</label>
      <div class="layui-input-block">
        <input type="fullName" name="fullName" placeholder="请输入币种全称" autocomplete="off" class="layui-input" lay-verify="required">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">合约地址</label>
      <div class="layui-input-block">
        <input type="contractAddress" name="contractAddress" placeholder="请输入合约地址" autocomplete="off" class="layui-input">
      </div>
    </div>
        <div class="layui-form-item">
      <label class="layui-form-label">合约精度</label>
      <div class="layui-input-block">
        <input type="contractPrecision" name="contractPrecision" placeholder="请输入合约精度" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">节点名称</label>
      <div class="layui-input-block">
        <input type="text" name="nodeFullName" id = "nodeFullName" placeholder="节点名称" autocomplete="off" class="layui-input" lay-verify="required" readonly = "readonly">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">状态</label>
      <div class="layui-input-block">
        <input type="checkbox" name="status"  lay-filter="switchChangeValue" class="layui-input" lay-skin="switch" lay-text="正常|禁用" checked>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">归集数量下限</label>
      <div class="layui-input-block">
        <input type="collectLimitMin" name="collectLimitMin" placeholder="请输入归集数量下限" autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">当前是否可归集</label>
      <div class="layui-input-block">
        <input type="checkbox" name="currentCollectFlag" lay-filter="switchChangeValue" lay-skin="switch" lay-text="是|否" checked class="layui-input">
      </div>
    </div>
       
    <div class="layui-form-item">
      <label class="layui-form-label">币种类型</label>
      <div class="layui-input-block">
        <select name="type" id="type" lay-verify="required">
			<option value="">--请选择--</option>
		</select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">资金池数量</label>
      <div class="layui-input-block">
        <input type="poolAmount" name="poolAmount" placeholder="请输入资金池数量" autocomplete="off" class="layui-input">
      </div>
    </div>
    
    <div class="layui-form-item">
      <label class="layui-form-label">是否可兑换</label>
      <div class="layui-input-block">
        <input type="checkbox" name="exchangeFlag" lay-filter="switchChangeValue" placeholder="请输入是否可兑换" autocomplete="off" class="layui-input" lay-skin="switch" lay-text="是|否" checked>
      </div>
    </div>
        
     
  <div class="layui-form-item">
  <div class="layui-input-block">
    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
    <button  class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
  </div>
</div>
</form>
</div>
</div>

<div class="table_div">
  <div id="searchParam"  shiro:hasPermission="sys:coin:list">
    <div class="layui-form-item">
      <div class="layui-input-inline">
        <input type="text" id="code" class="layui-input"  autocomplete="off" placeholder="币种代码">
      </div>

      <div class="layui-input-inline ">
        <button class="layui-btn" onclick="search()"  id="search">查询</button>
        <button class="layui-btn"   id="export">导出全部</button>
      </div>
    </div>

  </div>
  <table class="layui-table" id="showTable" lay-filter="showTable" ></table>
</div>
<script type="text/html" id="toolbar">
  <div class="layui-btn-container">
    <button class="layui-btn layui-btn-sm" lay-event="add"  shiro:hasPermission="sys:coin:add">添加</button>
    <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="sys:coin:delete">删除</button>
  </div>
</script>
<script type="text/html" id="tool">
  <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="sys:coin:update">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="sys:coin:delete">删除</a>
</script>

</body>

<div  th:replace="~{sys/common/common_res::common_js}"></div>
<script>
  //获取token
  var token = CoreUtil.getData("access_token");
  //地址栏转义token中的#号
  var tokenQuery = token.replace("#", "%23");
  var tableIns1;
  var table = layui.table;
  var form = layui.form;
  var layer = layui.layer;
  var $ = jQuery = layui.jquery;
  var laydate = layui.laydate;
  layui.config({
		base : '/layui/lay/modules/' //你的模块目录
  }); //加载入口
  layui.use(['table', 'layer', 'laydate','tableSelect'], function () {
	var dicts = CoreUtil.ajax("/sys/dictDetail/findByKeys?keys=sex,commonStatus,commonYesNo,coinType");
	
	selectDictShow("type",dicts.coinType,'0');
	form.render('select');//重新渲染
    //加载table
    tableIns1 = table.render({
      elem: '#showTable'
      , contentType: 'application/json'
      , headers: {"x_access_token": token}
      , page: true //开启分页
      , url: '/sys/coin/list' //数据接口
      , method: 'POST'
      ,cellMinWidth: 80
      , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
        return {
          "code": res.code, //解析接口状态
          "msg": res.msg, //解析提示文本
          "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
          "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
        }
      }
      , cols: [
        [
          {type: 'checkbox', fixed: 'left'},
          {field: 'id', title: 'ID', width: 100},
          {field: 'code', title: '币种代码', width: 100},
          {field: 'fullName', title: '币种全称', width: 120},
          {field: 'contractAddress', title: '合约地址', width: 120},
          {field: 'contractPrecision', title: '合约精度', width: 100},
          {field: 'status', title: '状态', templet: function (item) {
         	var value = item.status;
			var codeName = "";
			var dictData = dicts.commonStatus;
			$(dictData).each(function(i){
			   if(dictData[i].value == value){
			   		codeName = dictData[i].label;
			   		return;
			   }
			 });
			return codeName;
          }},
          {field: 'exchangeFlag', title: '是否可兑换', width: 100, templet: function (item) {
         	var value = item.status;
			var codeName = "";
			var dictData = dicts.commonYesNo;
			$(dictData).each(function(i){
			   if(dictData[i].value == value){
			   		codeName = dictData[i].label;
			   		return;
			   }
			 });
			return codeName;
          }},
          {field: 'collectLimitMin', title: '归集数量下限', width: 120},
          {field: 'poolAmount', title: '资金池数量', width: 100},
          {field: 'createTime', title: '创建时间', width: 160},
          {field: 'updateTime', title: '更新时间', width: 160},
          {field: 'createBy', title: '创建人'},
          {width: 120, toolbar: "#tool", title: '操作', fixed: 'right'}
        ]
      ]
      , toolbar: '#toolbar'
    });
    
  	//监听指定开关把on转为1 off转为0
    form.on('switch(switchChangeValue)', function(data){
    	if(this.checked) {
    		$(this).val("1")
    	} else {
    		$(this).val("0")
    	}
    });


    //表头工具
    table.on('toolbar(showTable)', function(obj){
      switch(obj.event){
        case 'batchDeleted':
          var checkStatus = table.checkStatus(obj.config.id);
          var data = checkStatus.data;
          if(data.length==0){
            layer.msg("请选择要批量删除的列");
          }else {
            var ids = [];
            $(data).each(function (index,item) {
              ids.push(item.id);
            });
            tipDialog(ids);
          }
          break;
        case 'add':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("新增");
          $(".operation input[name=id]").val("");
          $(".operation input[name=code]").val("");
          $(".operation input[name=fullName]").val("");
          $(".operation input[name=contractAddress]").val("");
          $(".operation input[name=contractPrecision]").val("");
          $(".operation input[name=sort]").val("");
          $(".operation input[name=status]").attr('checked', 'checked');
          $(".operation input[name=status]").val("1");
          $(".operation input[name=currentCollectFlag]").attr('checked', 'checked');
          $(".operation input[name=currentCollectFlag]").val("1");
          $(".operation input[name=exchangeFlag]").attr('checked', 'checked');
          $(".operation input[name=exchangeFlag]").val("1");
          $(".operation input[name=nodeId]").val("");
          $(".operation input[name=nodeFullName]").val("");
          $(".operation input[name=collectLimitMin]").val("");
          $(".operation input[name=collectTime]").val("");
          $(".operation input[name=createTime]").val("");
          $(".operation input[name=updateTime]").val("");
          $(".operation input[name=createBy]").val("");
          $(".operation input[name=type]").val("");
          $(".operation input[name=assetPrecision]").val("");
          $(".operation input[name=poolAmount]").val("");
          break;
      };
    });
    //列操作
    table.on('tool(showTable)',function (obj) {
      var data = obj.data;
      switch (obj.event) {
        case 'del':
          var ids=[];
          ids.push(data.id);
          tipDialog(ids);
          break;
        case 'edit':
          $(".table_div").hide();
          $(".operation").show();
          $(".title").html("编辑");
          $(".operation input[name=id]").val(data.id);
          $(".operation input[name=code]").val(data.code);
          $(".operation input[name=fullName]").val(data.fullName);
          $(".operation input[name=contractAddress]").val(data.contractAddress);
          $(".operation input[name=contractPrecision]").val(data.contractPrecision);
          $(".operation input[name=sort]").val(data.sort);
          if("1" == data.status) {
        	  $(".operation input[name=status]").val("1");
        	  $(".operation input[name=status]").attr('checked', 'checked');
          } else {
        	  $(".operation input[name=status]").val("0");
        	  $(".operation input[name=status]").removeAttr('checked');
          }
          if("1" == data.currentCollectFlag) {
        	  $(".operation input[name=currentCollectFlag]").val("1");
        	  $(".operation input[name=currentCollectFlag]").attr('checked', 'checked');
          } else {
        	  $(".operation input[name=currentCollectFlag]").val("0");
        	  $(".operation input[name=currentCollectFlag]").removeAttr('checked');
          }
          if("1" == data.exchangeFlag) {
        	  $(".operation input[name=exchangeFlag]").val("1");
        	  $(".operation input[name=exchangeFlag]").attr('checked', 'checked');
          } else {
        	  $(".operation input[name=exchangeFlag]").val("0");
        	  $(".operation input[name=exchangeFlag]").removeAttr('checked');
          }
          form.render(); //更新全部
          $(".operation input[name=nodeId]").val(data.nodeId);
          $(".operation input[name=nodeFullName]").val(data.nodeFullName);
          $(".operation input[name=collectLimitMin]").val(data.collectLimitMin);
          $(".operation input[name=collectTime]").val(data.collectTime);
          $(".operation input[name=createTime]").val(data.createTime);
          $(".operation input[name=updateTime]").val(data.updateTime);
          $(".operation input[name=createBy]").val(data.createBy);
          selectDictShow("type",dicts.coinType,data.type);
      	  form.render('select');//重新渲染
          $(".operation input[name=assetPrecision]").val(data.assetPrecision);
          $(".operation input[name=poolAmount]").val(data.poolAmount);
          break;
      }
    });

    //导出
    $('#export').on('click', function () {
      //原先分页limit
      var exportParams = {
        limit: 10000,
        key: $("#key").val()
      };
      CoreUtil.sendPost("/sys/coin/list", exportParams, function (res) {
        //初始化渲染数据
        if (res.data != null && res.data.records != null) {
          table.exportFile(tableIns1.config.id, res.data.records, 'xls');
        }
      });
    });

    //删除
    var tipDialog=function (ids) {
      layer.open({
        content: "确定要删除么?",
        yes: function(index, layero){
          layer.close(index); //如果设定了yes回调，需进行手工关闭
          CoreUtil.sendDelete("/sys/coin/delete",ids,function (res) {
            layer.msg(res.msg, {time:1000},function () {
              search();
            });
          });
        }
      });
    };

    //返回
    $("#btn_cancel").click(function() {
      $(".table_div").show();
      $(".operation").hide();
      return false;
    });

    //监听保存
    form.on('submit(submit)', function(data){
      if(data.field.id===undefined || data.field.id===null || data.field.id===""){
        CoreUtil.sendPost("/sys/coin/add",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }else {
        CoreUtil.sendPut("/sys/coin/update",data.field,function (res) {
          $(".table_div").show();
          $(".operation").hide();
          search();
        });
      }

      return false;
    });
   
    var tableSelect = layui.tableSelect;
    tableSelect.render({
        elem: '#nodeFullName',
        checkedKey: 'id',
        searchKey: 'name',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '节点名称',	//搜索输入框的提示文字 默认关键词搜索
        table: {
            height:200,
            url: '/sys/node/list?limit=1',
            method: 'POST',
            headers: {"x_access_token": token},
            contentType: 'application/json',
            cellMinWidth: 80,
            limits:[10,30],
            limit:10,
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                  "code": res.code, //解析接口状态
                  "msg": res.msg, //解析提示文本
                  "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                  "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
              },
            cols: [
            	[
                {type: 'radio'},
                {field: 'name', title: '节点名称'}
            ]
            	]
        },
        done: function (elem, data) {
            var NEWJSON = []
            layui.each(data.data, function (index, item) {
                $("#nodeId").val(item.id);
                NEWJSON.push(item.name)
            })
            elem.val(NEWJSON.join(","))
        }
    });
  });

  //执行查询
  function search() {
    //这里以搜索为例
    tableIns1.reload({
      where: { //设定异步数据接口的额外参数，任意设
        code: $("#code").val()
      }
      , page: {
        curr: 1 //重新从第 1 页开始
      }
    });
  };
</script>
</html>